package com.example.demo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.API.TheItemsGson;

import java.util.List;

public class MyAdapterTest extends RecyclerView.Adapter<MyAdapterTest.MyViewHolder> {
    //创建一个list类型的theItemsList
    private List<TheItemsGson.TheItemsListDTO> theItemsList;
    public void setData(List<TheItemsGson.TheItemsListDTO> theItemsList){
        this.theItemsList = theItemsList;
        notifyDataSetChanged();

    }


    //指定item布局
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_app2, parent, false);
        return new MyViewHolder(view);
    }
    //绑定数据
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //把数据绑定到item_my_app.xml中的TextView控件上面 显示出来
        //找到位置
        holder.tvRank.setText(theItemsList.get(position).getTvRank());
        //holder.img.setImageResource(theItemsList.get(position).getImg());
        holder.tvName.setText(theItemsList.get(position).getTvName());
        holder.tvStart.setText(theItemsList.get(position).getTvStart());
        holder.tvDowloadCount.setText(theItemsList.get(position).getTvDowloadCount());
        holder.button2.setText(theItemsList.get(position).getButton2());
    }
    //返回当前item数量
    @Override
    public int getItemCount() {
        //返回apps list的长度
        return theItemsList == null ? 0:theItemsList.size();
    }

    //复用布局
    static class MyViewHolder extends RecyclerView.ViewHolder{
        //找到item_my_app.xml中的TextView控件
        TextView tvRank;
        ImageView img;
        TextView tvName;
        TextView tvStart;
        TextView tvDowloadCount;
        TextView button2;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvRank = itemView.findViewById(R.id.tv_rank);
            img = itemView.findViewById(R.id.img);
            tvName = itemView.findViewById(R.id.tv_name);
            tvStart = itemView.findViewById(R.id.tv_start);
            tvDowloadCount = itemView.findViewById(R.id.tv_dowload_count);
            button2 = itemView.findViewById(R.id.button2);
        }
    }

}
